# $Id$
# Maintainer: Jan Alexander Steffens (heftig) <jan.steffens@gmail.com>
# Contributor: Daniel J Griffiths <ghost1227@archlinux.us>
# Contributor: Corrado Primier <bardo@aur.archlinux.org>
# Contributor: William Rea <sillywilly@gmail.com>

_pkgname="pulseaudio"
#pkgname="$_pkgname-git"
#pkgbase=pulseaudio
#pkgname=("${_pkgname}-git" libpulse)
pkgname=(${_pkgname} libpulse pulseaudio-{gconf,zeroconf,lirc,jack,bluetooth,equalizer})
#required name: pulseaudio NOT pulseaudio-git!!! because: //fixed(renamed pulseaudio-git to pulseaudio!)---- also, can't install! due to:
#sudo /usr/bin/pacman -U /home/z/build/1packages/pulseaudio-git/packages_pacman/pulseaudio-git-9.0.45.g4cd89aa-1-x86_64.pkg.tar /home/z/build/1packages/pulseaudio-git/packages_pacman/libpulse-9.0.45.g4cd89aa-1-x86_64.pkg.tar
#loading packages...
#resolving dependencies...
#looking for conflicting packages...
#error: failed to prepare transaction (could not satisfy dependencies)
#:: pulseaudio: installing libpulse (9.0.45.g4cd89aa-1) breaks dependency 'libpulse=8.0-3'
#==> WARNING: Failed to install built package(s).
pkgdesc="A featureful, general-purpose sound server"
pkgver=9.0.102.g2abcbd2
pkgrel=1
arch=(i686 x86_64)
url="http://www.freedesktop.org/wiki/Software/PulseAudio"
license=(LGPL)
makedepends=(git libasyncns libcap attr libxtst libsm libsndfile libtool rtkit libsoxr
             speexdsp tdb systemd dbus avahi 
            ######bluez bluez-libs 
             gconf intltool 
             #jack2-dbus
             sbc
             lirc openssl 
             #xenstore 
             fftw orc json-c gtk3 webrtc-audio-processing
             #check
             )
#depends=("rtkit" "libltdl" "speex" "tdb" "systemd" "fftw" "orc" "libsamplerate"
#         "webrtc-audio-processing" "sbc" "libasyncns" "libxtst" "libsm" "libsndfile" "json-c")
options=(!emptydirs)
#source=(http://freedesktop.org/software/$pkgbase/releases/$pkgbase-$pkgver.tar.xz
patch1='shm_open_whatfilename.patch'
source=("git+https://github.com/pulseaudio/pulseaudio.git"
        "$patch1"
        padsp-lib32.patch
        pulseaudio.install
        )
#sha256sums=('c3d3d66b827f18fbe903fe3df647013f09fc1e2191c035be1ee2d82a9e404686'
sha256sums=('SKIP'
            'fac6861a6ceda52238614a91a0319501a20c00687a6ad41389498e7fb987e8a6'
            '7832fc59df76538ff10aedd297c03cb7ff117235da8bfad26082994bb5b84332'
            '1d4890b10fadb9208c3fefbbed4aca1f22e63a0f102f4c598dc573a55e724cb2')

pkgver() {
    cd "$srcdir/$_pkgname"
    git describe --always | sed -e "s/-/./g" -e 's/^v//'
}

#export CFLAGS="-pipe -march=native -Wno-trigraphs -fno-schedule-insns2 -fno-delete-null-pointer-checks -fomit-frame-pointer -O2"
#export CPPFLAGS="${CFLAGS}"
#export CXXFLAGS="${CFLAGS}"


prepare() {
  #cd $pkgbase-$pkgver
  cd "$srcdir/$_pkgname"
  patch -Np1 -i "${srcdir}/$patch1"

#  ./configure --prefix=/usr \
  ./autogen.sh --prefix=/usr \
    --sysconfdir=/etc \
    --libexecdir=/usr/lib \
    --localstatedir=/var \
    --with-udev-rules-dir=/usr/lib/udev/rules.d \
    --with-database=tdb \
        --disable-hal-compat \
    --disable-tcpwrap \
    --disable-bluez4 \
    --disable-samplerate \
    --disable-rpath \
    --disable-default-build-tests \
    DATADIRNAME=share


  # fight unused direct deps
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
}

build() {
  #cd $pkgbase-$pkgver
  cd "$srcdir/$_pkgname"

  make
}

package_pulseaudio() {
  depends=("libpulse=$pkgver-$pkgrel" rtkit libltdl speexdsp tdb orc
           webrtc-audio-processing sbc libsoxr)
  optdepends=('pulseaudio-alsa: ALSA configuration (recommended)')
  backup=(etc/pulse/{daemon.conf,default.pa,system.pa})
  install=pulseaudio.install

  #cd $pkgbase-$pkgver
  cd "$srcdir/$_pkgname"

  _pulsever=$(./git-version-gen .tarball-version)
  while [[ $_pulsever = *.*.* ]]; do
    _pulsever=${_pulsever%.*}
  done
  _pulsever=${_pulsever%%-*}

  make -j1 DESTDIR="$pkgdir" install \
    bashcompletiondir=/usr/share/bash-completion/completions

  cd "$pkgdir"
  patch -Np1 -i "$srcdir/padsp-lib32.patch"

  # Assumes that any volume adjustment is intended by the user, who can control
  # each app's volume. Misbehaving clients can trigger earsplitting volume
  # jumps. App volumes can diverge wildly and cause apps without their own
  # volume control to fall below sink volume; a sink-only volume control will
  # suddenly be unable to make such an app loud enough.
  sed -e '/flat-volumes/iflat-volumes = no' \
      -i etc/pulse/daemon.conf

  # Superseded by socket activation
  sed -e '/autospawn/iautospawn = no' \
      -i etc/pulse/client.conf

  # Disable cork-request module, can result in e.g. media players unpausing
  # when there's a Skype call incoming
  sed -e 's|/usr/bin/pactl load-module module-x11-cork-request|#&|' \
      -i usr/bin/start-pulseaudio-x11

  rm etc/dbus-1/system.d/pulseaudio-system.conf

### Split libpulse

  mkdir -p "$srcdir"/libpulse/{etc/pulse,usr/{bin,lib/pulseaudio,share/man/man{1,5}}}

  mv etc/pulse/client.conf \
     "$srcdir/libpulse/etc/pulse"

  mv usr/bin/pa{cat,ctl,dsp,mon,play,rec,record} \
     "$srcdir/libpulse/usr/bin"

  mv usr/lib/libpulse{,-simple,-mainloop-glib}.so* \
     "$srcdir/libpulse/usr/lib"
     #usr/lib/{cmake,pkgconfig} \

  mv usr/lib/pulseaudio/libpulse{dsp,common-*}.so \
     "$srcdir/libpulse/usr/lib/pulseaudio"

#  mv usr/include \
#     "$srcdir/libpulse/usr"

  mv usr/share/man/man1/pa{cat,ctl,dsp,mon,play,rec,record}.1 \
     "$srcdir/libpulse/usr/share/man/man1"

  mv usr/share/man/man5/pulse-client.conf.5 \
     "$srcdir/libpulse/usr/share/man/man5"

#  mv usr/share/vala \
#     "$srcdir/libpulse/usr/share"

### Split modules

  mkdir -p "$srcdir"/{gconf,zeroconf,lirc,jack,bluetooth,equalizer}/usr/lib/pulse-$_pulsever/modules \
           "$srcdir"/{gconf/usr/lib/pulse,equalizer/usr/bin}

  mv usr/lib/pulse-$_pulsever/modules/module-gconf.so \
     "$srcdir/gconf/usr/lib/pulse-$_pulsever/modules"
  mv usr/lib/pulse/gconf-helper \
     "$srcdir/gconf/usr/lib/pulse"

  mv usr/lib/pulse-$_pulsever/modules/{libavahi-wrap,module-{zeroconf-{publish,discover},raop-discover}}.so \
     "$srcdir/zeroconf/usr/lib/pulse-$_pulsever/modules"

  mv usr/lib/pulse-$_pulsever/modules/module-lirc.so \
     "$srcdir/lirc/usr/lib/pulse-$_pulsever/modules"

#  mv usr/lib/pulse-$_pulsever/modules/module-xenpv-sink.so \
#     "$srcdir/xen/usr/lib/pulse-$_pulsever/modules"

  mv usr/lib/pulse-$_pulsever/modules/module-jack{-sink,-source,dbus-detect}.so \
     "$srcdir/jack/usr/lib/pulse-$_pulsever/modules"

  mv usr/lib/pulse-$_pulsever/modules/{libbluez5-util,module-{bluetooth-{discover,policy},bluez5-{discover,device}}}.so \
     "$srcdir/bluetooth/usr/lib/pulse-$_pulsever/modules"

  mv usr/lib/pulse-$_pulsever/modules/module-equalizer-sink.so \
     "$srcdir/equalizer/usr/lib/pulse-$_pulsever/modules"
  mv usr/bin/qpaeq \
     "$srcdir/equalizer/usr/bin"
}

package_libpulse() {
  pkgdesc="$pkgdesc (client library)"
  depends=(dbus libasyncns libcap libxtst libsm libsndfile json-c systemd)
  backup=(etc/pulse/client.conf)

  mv "$srcdir"/libpulse/* "$pkgdir"
}

package_pulseaudio-gconf(){
  pkgdesc="GConf support for PulseAudio"
  depends=("pulseaudio=$pkgver-$pkgrel" gconf)

  mv "$srcdir"/gconf/* "$pkgdir"
}

package_pulseaudio-zeroconf(){
  pkgdesc="Zeroconf support for PulseAudio"
  depends=("pulseaudio=$pkgver-$pkgrel" avahi openssl)

  mv "$srcdir"/zeroconf/* "$pkgdir"
}

package_pulseaudio-lirc(){
  pkgdesc="IR (lirc) support for PulseAudio"
  depends=("pulseaudio=$pkgver-$pkgrel" lirc)

  mv "$srcdir"/lirc/* "$pkgdir"
}

#package_pulseaudio-xen(){
#  pkgdesc="Xen support for PulseAudio"
#  depends=("pulseaudio=$pkgver-$pkgrel" xenstore)
#  license=(GPL2)
#
#  mv "$srcdir"/xen/* "$pkgdir"
#}

package_pulseaudio-jack(){
  pkgdesc="Jack support for PulseAudio"
  depends=("pulseaudio=$pkgver-$pkgrel" jack2)

  mv "$srcdir"/jack/* "$pkgdir"
}

package_pulseaudio-bluetooth(){
  pkgdesc="Bluetooth support for PulseAudio"
  depends=("pulseaudio=$pkgver-$pkgrel" bluez bluez-libs)

  mv "$srcdir"/bluetooth/* "$pkgdir"
}

package_pulseaudio-equalizer(){
  pkgdesc="Equalizer for PulseAudio"
  depends=("pulseaudio=$pkgver-$pkgrel" python-pyqt4 fftw)
  license=(AGPL3)

  mv "$srcdir"/equalizer/* "$pkgdir"
}
